Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
## Part-3 Making Things Happen ## Chapter 11 Working With Text Many programs that are created with Visual Basic deal with text in one way or another. In this chapter, we’ll explore Visual Basic’s text processing capabilities by creating a handy text editor. Have you ever seen a program that doesn’t deal with text in one form or another? They’re pretty rare, that’s for sure. Some programs, such as word processors, are designed specifically to work with text. Others, such as databases, use text as a means for displaying and organizing data. Regardless of the type of Visual Basic project you end up tackling, you’ll undoubtedly be dealing with text on a regular basis. Fortunately for us, Visual Basic has a variety of powerful, text-related tools. In this chapter’s project, we’ll use these tools to create a fully functional text editor. In addition, I’ll show you some other important capabilities, including: • Storing text data in disk files • Adding a menu to a Visual Basic program • Using the Common Dialog control • Creating a project that uses multiple forms When we’re finished, you’ll know a lot more about Visual Basic, and you’ll have a pretty nice text editor. You can use the editor as a standalone program or as a software component you can add to your other Visual Basic projects that require text-editing capabilities. ### Planning The Editor As always, it pays to take a little time to plan the project before setting fingers to keyboard. We are designing a text editor, which is quite different from a word processor. Word processors, such as Microsoft Word and WordPerfect, include an assortment of complex features for creating professionally formatted documents—styles, footnotes, graphics, and so on. Our project is much less ambitious. A text editor does nothing more than edit text files—that is, add, delete, and rearrange the text in the file. It offers no formatting or automatic word wrap (you must press Enter to start a new line)—no fancy stuff. TIP: Understanding Text Files What exactly is a text file? A text file contains only the so-called standard characters: letters, numbers, punctuation marks, and so on. Text files are sometimes called ASCII files; the American Standard Code for Information Interchange (ASCII) specifies which numbers are used to represent characters. (Remember, computers use numbers internally for data storage.) For example, the letter a is represented by the number 97, and the percent symbol is represented by the number 37. You can find a table of ASCII codes in the Visual Basic Help system. How do you tell a text file from a non-text file (such as a program file or a word processor document)? Sometimes, the file-name extension can help. The .TXT extension is (or at least should be) reserved for text files. DOS batch files (BAT), Windows INI files (INI), and all Visual Basic program files (VBP, FRM, and BAS) are text files, too. The ultimate test, however, is to view the file’s contents, either by using a text editor or the Type command from the DOS prompt. If the file displays nothing but recognizable characters, it’s a text file. The editor we will create is what I call a Baby Editor—one that provides only the most basic editing functions. It will be quick, simple to use, and easy to customize, should the need arise. What features should it have? Here are the bare essentials I would want in any editor: • Basic editing: inserting and deleting text, moving the cursor, etc. • Text selection capabilities • Cut, copy, and paste operations • Capability to open any user-specified file • Capability to save a file under its original name or a new name • Never losing unsaved changes without warning the user As you might suspect, we will include all these features in our sample program. I have also added the ability to select the font in which the text is displayed. You may have a different list of “must have” features. That’s one of the beauties of the Baby Editor: You can easily customize it. ### Programming The Editor Let’s start with the editor’s basic text-handling chores: entering and editing text. If you think this sounds like it involves some pretty serious programming, you are in for a pleasant surprise. All you have to do is drop a Text Box control on a form, and most of the work is finished. That’s right, many of the text-handling capabilities we need are already built into the Text Box control, including cursor movement, selecting text, and inserting and deleting characters. We only have to add the ability to cut, copy, and paste text (using the Clipboard) to complete the editing part of the project. #### Starting The Project In Visual Basic, start a new Standard EXE project. Resize the form to the size you would like it to be when the program runs (the user can always change it) and set its properties as follows:
Name | frmBabyEditor
Caption | Text Editor - untitled
Next, place a Text Box control on the form. Don’t worry about the size and position right now. When we execute the program, the Text Box will be adjusted in code to fill the entire form. Set the Text Box properties as shown here:
Multiline | True
Scrollbars | Both
Text | (nothing)
These property settings give us a Text Box that can hold multiple lines of text (as opposed to the default, a single line) and has scrollbars that permit the user to scroll the text both vertically and horizontally using the mouse (the default is no scrollbars). Leave the other properties at their default settings. (Throughout the book, if I do not specifically say to change a property, assume that the default setting is correct or that we will change the setting in code.) Next, add a Common Dialog control to the form. This control provides several of Windows’ standard dialog boxes; we’ll see how it works later in the chapter. If the Common Dialog icon is not present in your toolbox, select Components from the Project menu, click on the Controls tab, and click on the box next to the Microsoft Common Dialog Control entry, so a check mark appears in it. Then, close the dialog box. Figure 11.1 The Baby Editor with the Text Box and Common Dialog controls added. Your form should look something like the one shown in Figure 11.1. The Common Dialog control displays as a small icon on the form during program design. Now is a good time to save your project. I suggest that you use the name BabyEditor for both the form and the project.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.